函式的預設引數default arguments
:在定義函數時為某些參數設置的預設值,當呼叫函數時省略呼叫該參數,就會使用預設值
def greet(name, greeting="Hello"):
print(f"{greeting}, {name}")
greet("Rose")
Hello, Rose
def greet(name, greeting="Hello"):
print(f"{greeting}, {name}")
greet("Rose", "Hi")
Hi, Rose
關鍵字參數 Keyword Arguments
def hello(greeting, title, first_name, last_name):
print(f"{greeting}, {title}, {first_name}, {last_name}")
hello("Hello~", "Miss", "Tina", "Wang")
Hello~ Miss Tina Wang
輸入參數數值
def hello(greeting, title, first_name, last_name):
print(f"{greeting}, {title}, {first_name}, {last_name}")
hello(greeting="Hello~~", title="Mr.",first_name="Jhon", last_name="Chen")
Hello~~ Mr. Jhon Chen